/****************************************************************************
 * 2025.10 XLB-20250122JTG
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;
using UnityEngine.U2D;

namespace ProjectSurvivor
{
	public partial class UnlockedIconPanel : UIElement, IController
	{
		private Dictionary<string, System.Tuple<ExpUpgradeItem, Image>> mUnlockedKeys = 
			new Dictionary<string, System.Tuple<ExpUpgradeItem, Image>>();
		
		private ResLoader mResLoader = ResLoader.Allocate();
		private void Awake()
		{
			UnlockedIconTemplate.Hide();
			var IconAtlas = mResLoader.LoadSync<SpriteAtlas>("icon");
			
			foreach (var expUpgradeItem in this.GetSystem<ExpUpgradeSystem>().Items)
			{
				var cacheItem = expUpgradeItem;
				
				cacheItem.CurrentLevel.RegisterWithInitValue(level =>
				{
					if (level > 0)
					{
						if (mUnlockedKeys.ContainsKey(cacheItem.Key))
						{
						}
						else
						{
							UnlockedIconTemplate.InstantiateWithParent(UnlockedIconRoot)
								.Self(self =>
								{
									self.sprite = IconAtlas.GetSprite(cacheItem.IconName);
									mUnlockedKeys.Add(cacheItem.Key,
										new System.Tuple<ExpUpgradeItem, Image>(cacheItem, self));
								})
								.Show();
						}
					}
				}).UnRegisterWhenGameObjectDestroyed(gameObject);
			}
			
			Global.SuperSimpleSword.Register(unlocked =>
			{
				if (unlocked)
				{
					if (mUnlockedKeys.ContainsKey("simple_sword"))
					{
						var item = mUnlockedKeys["simple_sword"].Item1;
						var sprite = IconAtlas.GetSprite(item.PairedIconName);
						mUnlockedKeys["simple_sword"].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			Global.SuperSimpleRotateSword.Register(unlocked =>
			{
				if (unlocked)
				{
					if (mUnlockedKeys.ContainsKey("rotate_sword"))
					{
						var item = mUnlockedKeys["rotate_sword"].Item1;
						var sprite = IconAtlas.GetSprite(item.PairedIconName);
						mUnlockedKeys["rotate_sword"].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			Global.SuperSimpleKnife.Register(unlocked =>
			{
				if (unlocked)
				{
					if (mUnlockedKeys.ContainsKey("simple_knife"))
					{
						var item = mUnlockedKeys["simple_knife"].Item1;
						var sprite = IconAtlas.GetSprite(item.PairedIconName);
						mUnlockedKeys["simple_knife"].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
			
			Global.SuperSimpleBall.Register(unlocked =>
			{
				if (unlocked)
				{
					if (mUnlockedKeys.ContainsKey("simple_ball"))
					{
						var item = mUnlockedKeys["simple_ball"].Item1;
						var sprite = IconAtlas.GetSprite(item.PairedIconName);
						mUnlockedKeys["simple_ball"].Item2.sprite = sprite;
					}
				}
			}).UnRegisterWhenGameObjectDestroyed(gameObject);
		}

		protected override void OnBeforeDestroy()
		{
			mResLoader.Recycle2Cache();
			mResLoader = null;
			mUnlockedKeys.Clear();
		}

		public IArchitecture GetArchitecture()
		{
			return Global.Interface;
		}
	}
}